package businessServlets;


import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.*;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;
import org.apache.commons.io.IOUtils;
import org.omg.CORBA.portable.Streamable;


import com.google.appengine.api.datastore.Email;
import com.google.appengine.api.datastore.PhoneNumber;
import com.google.appengine.api.images.Image;
import com.google.appengine.api.images.ImagesService;
import com.google.appengine.api.images.ImagesServiceFactory;
import com.google.appengine.api.images.Transform;

import controller.Controller;

@SuppressWarnings("serial")
public class CreateAccountBusiness2 extends HttpServlet {
	
	ImagesService imagesService = ImagesServiceFactory.getImagesService(); 
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/plain");
		resp.getWriter().println("sign in get =)");
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String tmp =null;
		int counter=0;
		while((tmp = req.getParameter("list_"+counter))!=null){
			//System.out.println(tmp);
			resp.getWriter().println(tmp);
			counter++;
		}
		resp.getWriter().println(counter);
		counter=0;
		while((tmp = req.getParameter("message_"+counter))!=null){
			//System.out.println(tmp);
			resp.getWriter().println(tmp);
			counter++;
		}
		//resp.sendRedirect("http://www.google.com");
		//resp.setContentType("text/plain");
		
		/*boolean isMultipart = ServletFileUpload.isMultipartContent(req);
		System.out.println(isMultipart);
		
		
		if (!isMultipart)
		{
			String userName = req.getParameter("userName");
			req.getSession().setAttribute("userNameBusiness", userName);
			String password = req.getParameter("password");
			String password1 = req.getParameter("password1");
			Email email = new Email(req.getParameter("email"));
			String businessName = req.getParameter("businessName");
			String businessNumber = req.getParameter("businessNumber");
			PhoneNumber phone = new PhoneNumber(req.getParameter("phone"));
			
			
			System.out.println(userName + " " + password + " " + password1 + " "+ email.getEmail() + " "+ businessName + " "+  businessNumber +" " + phone.getNumber());
			
			ArrayList<String> status = new ArrayList<String>();
			int i=1;
			
			while (req.getParameter("statusItem"+i)!=null)
			{
				System.out.println("statusItem"+req.getParameter("statusItem"+i));
				status.add(req.getParameter("statusItem"+i));
				i++;
			}
			
			ArrayList<String> message = new ArrayList<String>();
			i=1;
			
			while (req.getParameter("messageItem"+i)!=null)
			{
				System.out.println("messageItem"+req.getParameter("messageItem"+i));
				message.add(req.getParameter("messageItem"+i));
				i++;
			}
			
			Controller.getInstance().newBisAccount(businessName, businessNumber, null, phone, email, userName, password, status, null, message);
			
			//System.out.println(req.getParameter("logo"));
			
			
			//resp.getWriter().println(req.getParameter("userName")+", welcome! =)");
			//resp.sendRedirect("/addOrder.jsp");
		}
		else
		{
			try { 
				ServletFileUpload upload = new ServletFileUpload();       
				resp.setContentType("text/plain");
				
				FileItemIterator iterator = upload.getItemIterator(req);       
				while (iterator.hasNext()) {         
					FileItemStream item = iterator.next(); 
					InputStream stream = item.openStream();
				
					
					if (item.isFormField()) {
						String name = item.getFieldName();
						System.out.println("form field " + name);
						System.out.println(Streams.asString(stream));
						//byte[] buffer = new byte[10];
						//stream.read(buffer);
						//System.out.println(buffer);
						//String value = ((FileItem) item).getString();
						
					}
					else
					{
						
						try
						{
							int len;           
							byte[] data = IOUtils.toByteArray(stream);
							
							
							
							Image oldImage = ImagesServiceFactory.makeImage(data); //exception if not image
							Transform resize = ImagesServiceFactory.makeResize(100, 200);
							Image newImage = imagesService.applyTransform(resize, oldImage); 
							//resp.getWriter().write("aaa");;
							
							
							//resp.getOutputStream().write("aaa\n".getBytes());
							//resp.setContentType("image/jpeg");
							resp.getOutputStream().write(newImage.getImageData(), 0, newImage.getImageData().length);
							while ((len = stream.read(buffer, 0, buffer.length)) != -1) {             
								//resp.getOutputStream().write(buffer, 0, len);
								
							}
							
							//statusItem1+
							//messageItem1+  until null
						}catch(Exception e)
						{
							System.out.println("not image");
						}
						
					}
				}
			} catch (Exception ex) {
				throw new ServletException(ex);     
			}
		} // end if
		
		
		resp.sendRedirect("/allOrders.jsp");*/
	} // end function
		

	
}
